extends Node

var sceneFileDict

var localUserName = ""
var currentUser
var currentRoom
var score = 0
var widthRegion = 160
var heightRegion = 320

func _ready():
	sceneFileDict = {
		"Title": preload("res://Scenes/UI/Title.tscn"),
		"RoomList": preload("res://Scenes/UI/RoomList.tscn"),
		"RoomItem": preload("res://Scenes/UI/RoomItem.tscn"),
		"Room": preload("res://Scenes/UI/Room.tscn"),
		"RoomMemberItem": preload("res://Scenes/UI/RoomMemberItem.tscn"),
		"MainGameMultiplayer": preload("res://Scenes/MainGameMultiplayer.tscn"),
		"Tetromino": preload("res://Scenes/Game/Tetromino.tscn"),
		"Block": preload("res://Scenes/Block.tscn"),
		"OtherPlayerViewTexture": preload("res://Scenes/Game/OtherPlayerViewTexture.tscn"),
		"OtherPlayerViewport": preload("res://Scenes/Game/OtherPlayerView.tscn"),
		"RankItem": preload("res://Scenes/UI/RankItem.tscn"),
		"Rank": preload("res://Scenes/UI/Rank.tscn"),
		"MainGameSinglePlayer": preload("res://Scenes/Game/MainGameSinglePlayer.tscn"),
		"TetrominoMoveSound": preload("res://Sound/TetrominoMoveSound.wav"),
		"TetrominoRotateSound": preload("res://Sound/TetrominoRotateSound.wav"),
		"DestroyLineSound": preload("res://Sound/DestroyLineSound.wav"),
		"CantDoSound": preload("res://Sound/CantDoSound.wav"),
		"TetrominoReachBottom": preload("res://Sound/TetrominoReachBottom.wav"),
		"Click": preload("res://Sound/Click.wav"),
		"GamingBgMusic": preload("res://Sound/GamingBgMusic.wav")
	}


func changeScrennOrientation2Landscape():
	DisplayServer.screen_set_orientation(DisplayServer.SCREEN_LANDSCAPE)


func changeScrennOrientation2Portrait():
	DisplayServer.screen_set_orientation(DisplayServer.SCREEN_PORTRAIT)


func changeScene(sceneName):
	get_tree().current_scene.queue_free()
	var sceneInstance = sceneFileDict[sceneName].instantiate() 
	get_tree().root.add_child(sceneInstance) 
	get_tree().current_scene = sceneInstance 


func createSceneInst(sceneName):
	return sceneFileDict[sceneName].instantiate() 
	

func getSoundStream(soundName):
	return sceneFileDict[soundName]
	
	
func isOSWindows():
	var osName = OS.get_name()
	return osName == "Windows"
	
	
func setCurrentRoomData(room):
	currentRoom = room
	
	
func intelligentLayout(containerWidth, containerHeight, count):
	var widthPerCell = int(containerWidth * 1.0 / count)
	var ratioCell = widthPerCell * 1.0 / containerHeight
	var ratioTetrisRegion = widthRegion * 1.0 / heightRegion
	
	var elementPosX
	var elementPosY
	var elementWidth
	var elementHeight
	if ratioCell > ratioTetrisRegion:
		elementHeight = containerHeight
		elementWidth = int(elementHeight * ratioTetrisRegion)
		elementPosY = 0
		elementPosX = (widthPerCell - elementWidth) / 2
	else:
		elementWidth = widthPerCell
		elementHeight = int(elementWidth / ratioTetrisRegion)
		elementPosX = 0
		elementPosY = (containerHeight - elementHeight) / 2
		
	var result = []
	for index in range(count):
		var element = {
			"x": elementPosX + index * widthPerCell,
			"y": elementPosY,
			"width": elementWidth,
			"height": elementHeight
		}
		result.append(element)
	
	return result


func exitGame():
	get_tree().quit()
	
	
	
